const log = require('../../util/log')
const config = require('../../util/config')
const chromeMng = require('../chromeMng.20180824')
const {
  Network,
  Page,
  DOM,
  Runtime,
  Emulation
} = chromeMng.client;

async function loginJd(obj) {
  obj = obj || {}
  obj.times = (obj.times || 0) + 1
  if (obj.times > config.chromeMaxTimes) {
    return new Promise((resolve, reject) => {
      reject('超时')
    })
  }
  log('logining jd')
  try {
    Page.navigate({
      url: 'https://media.jd.com/'
    })
    return new Promise((resolve, reject) => {
      step1(obj).then(result => {
        chromeMng.code = ''
        chromeMng.codeRequestId = 0
        chromeMng.codeText = ''
        resolve(result)
      }, result => {
        chromeMng.code = ''
        chromeMng.codeRequestId = 0
        chromeMng.codeText = ''
        reject(result)
      })
    })
  } catch (err) {
    log(err)
  }
}

async function step1(obj) {
  obj.times++
  if (obj.times > config.chromeMaxTimes) {
    return new Promise((resolve, reject) => {
      reject('超时')
    })
  }
  log('logining jd step 1')
  try {
    var {result} = await Runtime.evaluate({expression: 'location.href'})
    log(result.value)
    if (result.value == 'https://media.jd.com/') {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          step2(obj).then(result => {
            resolve(result)
          }, result => {
            reject(result)
          })
        }, 1000)
      })
    }
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        loginJd(obj).then(result => {
          resolve(result)
        }, result => {
          reject(result)
        })
      }, 1000)
    })
  } catch (err) {
    log(err)
  }
}

async function step2(obj) {
  obj.times++
  if (obj.times > config.chromeMaxTimes) {
    return new Promise((resolve, reject) => {
      reject('系统错误')
    })
  }
  log('logining jd step 2')
  try {
    var {result} = await Runtime.evaluate({expression: 'location.href'})
    if (result.value != 'https://media.jd.com/') {
      log('登录成功跳转')
      return new Promise((resolve, reject) => {
        resolve()
      })
    }
    var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("#userPin")'})
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 1) {
      var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("#userPin")[0].innerText'})
      if (result.value != '') {
        log('检测到用户名', result)
        return new Promise((resolve, reject) => {
          resolve()
        })
      }
    }
    var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")'})
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 0) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          step2(obj).then(result => {
            resolve(result)
          }, result => {
            reject(result)
          })
        }, 1000)
      })
    }
    if (cnt > 1) {
      return new Promise((resolve, reject) => {
        log('iframe多于1个')
        reject('系统错误')
      })
    }
    var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#o-authcode")'})
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 1) {
      var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#o-authcode")[0].className'})
      if (result.value.indexOf('hide') == -1) {
        if (!chromeMng.codeText) {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              step2(obj).then(result => {
                resolve(result)
              }, result => {
                reject(result)
              })
            }, 1000)
          })
        }
        await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#authcode")[0].value="' + chromeMng.codeText + '"'})
      }
      var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#o-authcode")[0].style.display'})
      if (result.value == 'block') {
        if (!chromeMng.codeText) {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              step2(obj).then(result => {
                resolve(result)
              }, result => {
                reject(result)
              })
            }, 1000)
          })
        }
        await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#authcode")[0].value="' + chromeMng.codeText + '"'})
      }
    }
    var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#loginname")'})
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 0) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          step2(obj).then(result => {
            resolve(result)
          }, result => {
            reject(result)
          })
        }, 1000)
      })
    }
    await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#loginname")[0].value="etourly"'})
    await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#nloginpwd")[0].value="yoyo9876"'})
    await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#paipaiLoginSubmit")[0].click()'})
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        step2(obj).then(result => {
          resolve(result)
        }, result => {
          reject(result)
        })
      }, 1000)
    })
  } catch (err) {
    log(err)
  }
}

module.exports = loginJd
